package com.zhonglian.elisa.customer;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.zhonglian.elisa.R;
import com.zhonglian.elisa.utils.LogUtil;

/**
 * Created : Yx
 * Time : 2020/3/24 17:22
 * Describe :十字架
 */
public class CrossView extends View {

    private int width, height;//屏幕宽高
    private int vWidth, vHeight;//View宽高
    private int miLength = 300, miWidth = 2, miColor = Color.RED;//线长.线粗.线颜色
    private int mdRadius = 4, mdColor = Color.BLUE;//中心圆点半径,圆点颜色
    private int cx, cy;//中心圆点此时坐标
    private Paint m_paint, d_paint;//画笔
    //横竖线坐标
    private int startX, startY, stateHeight;
    //按下时坐标
    private int memonyX;/*the last time the coordinate values of X*/
    private int memonyY;/*the last time the coordinate values of Y*/
    //移动后坐标
    private int pressX;/*X coordinate values while finger press*/
    private int pressY;/*Y coordinate values while finger press*/

    public CrossView(Context context) {
        super(context);
        init(context, null);
    }

    public CrossView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CrossView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public static int dpToPx(int dp) {
        float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }


    private void init(Context context, AttributeSet attrs) {
        //线
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CrossView);
        int miHeight = (int) a.getDimension(R.styleable.CrossView_cvHeight, 220);
        miLength = (int) a.getDimension(R.styleable.CrossView_cvLength, 300);
        miWidth = (int) a.getDimension(R.styleable.CrossView_cvWidth, 2);
        miColor = a.getColor(R.styleable.CrossView_cvColor, Color.RED);
        m_paint = new Paint();
        m_paint.setColor(miColor);
        m_paint.setStrokeWidth(miWidth);
        m_paint.setAntiAlias(true);
        //点
        mdColor = Color.RED;
        d_paint = new Paint();
        d_paint.setColor(mdColor);
        d_paint.setStrokeWidth(mdRadius);
        d_paint.setAntiAlias(true);
        if (miWidth > mdRadius * 2) mdRadius = miWidth / 2;
        a.recycle();
        //屏幕宽高
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getRealMetrics(dm);//手机真实分辨率。
        width = dm.widthPixels;
        height = dm.heightPixels;
        //View宽高
        vWidth = width;
        vHeight = miHeight;
        // 1.获取状态栏的高度
        Rect frame = new Rect();
        getWindowVisibleDisplayFrame(frame);
        stateHeight = frame.top;
        //初始化坐标
        //1.1初始化线条开始结束坐标
        startX = vWidth / 2;
        startY = vHeight / 2;
        //1.2初始化圆心点开始坐标
        cx = vWidth / 2;
        cy = vHeight / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        methods(canvas);
    }


    private void methods(Canvas canvas) {
        // 1.画圆心
        canvas.drawCircle(cx, cy, mdRadius, d_paint);
        //LogUtil.iYx("此时圆心是：" + cx + "/" + cy);
        // 画横线
        canvas.drawLine(0, startY, vWidth, startY, m_paint);
        //画竖线
        canvas.drawLine(startX, 0, startX, vHeight, m_paint);

    }


    @SuppressWarnings("NullableProblems")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (listener != null) {
                    listener.onTouch_Focus();
                }
                //按下坐标
                memonyX = (int) event.getX();
                memonyY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (listener != null) {
                    listener.onTouch_Focus();
                }
                //移动后坐标
                pressX = (int) event.getX();
                pressY = (int) event.getY();
                moveByTouch(pressX, pressY);
                postInvalidate();
                break;
            case MotionEvent.ACTION_UP:
                if (listener != null) {
                    listener.onTouch_UP();
                }
                //mPaintLine.setColor(Color.WHITE);
                postInvalidate();
                break;
            default:
                break;
        }
        return true;
    }

    /*随手指移动 mx my移动后坐标*/
    private void moveByTouch(int mx, int my) {/*move center point*/
        int dX = mx - memonyX;//移动后坐标 - 按下时坐标 = 移动了距离
        int dY = my - memonyY;
        cx += dX;
        cy += dY;
        memonyX = mx;
        memonyY = my;
        startX = cx;
        startY = cy;
    }


    private CrossViewListener listener;

    public void setCrossViewListener(CrossViewListener listener) {
        this.listener = listener;
    }

    /**
     * 为解决ScrollView冲突而诞生的一个操作
     * 设置MyScrollView是否处理滑动事件
     */
    public interface CrossViewListener {
        void onTouch_Focus();

        void onTouch_UP();

    }


}
